<?php
namespace app\tools\logic;
use think\facade\Db;

class UserLogic{

	//获取管理员列表
	static function getList(){
		$page_size = input('page_size', 10);
		$user_name = input('user_name', '');
		$obj = Db::name('auth_user')
					->alias('au')
					->join('auth_group ag','ag.id=au.auth_group_id','left')
					->field('au.id,au.user_name,au.phone,au.auth_group_id,ag.group_name,au.status,au.add_time')
					->where('au.status','<>',127);
		$user_name && $obj->where('user_name','like','%'.$user_name.'%');
		$list = $obj->order('au.id desc')
					->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id','');
		if(!$id){
			return 110;
		}
		$info = Db::name('auth_user')
					->field('id,user_name,phone,auth_group_id,status,add_time')
					->where('id',$id)
					->find();
		return $info;
	}

	static function save(){
		$id = input('id');
		$user_name = input('user_name');
		$phone = input('phone');
		$auth_group_id = input('auth_group_id');
		$password = input('password');
		$status = input('status', 1);
		if(!$id){
			return 110;
		}
		if(!$user_name){
			return 111;
		}
		if(!$phone){
			return 112;
		}

		$info = Db::name('auth_user')
				->field('id,salt')
				->where('id', $id)
				->find();
		if(!$info){
			return 113;
		}

		$data = [];
		$data['user_name'] = $user_name;
		$data['phone']	= $phone;
		$auth_group_id && $data['auth_group_id'] = $auth_group_id;
		$data['status'] = $status;
		if($password){
			//修改密码
			$data['password'] = md5(md5($password).$info['salt']);
		}
		$rs = Db::name('auth_user')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}

	}
	//新增
	static function add(){
		$user_name = input('user_name');
		$phone = input('phone');
		$auth_group_id = input('auth_group_id');
		$password = input('password');
		$status = input('status', 1);
		if(!$user_name){
			return 111;
		}
		if(!$phone){
			return 112;
		}
		if(!$password){
			return 114;
		}

		$data = [];
		$data['user_name'] = $user_name;
		$data['phone']	= $phone;
		$data['auth_group_id'] = $auth_group_id;
		$data['salt'] = get_chu();
		$data['password'] = md5(md5($password).$data['salt']);
		$data['status'] = $status;
		$data['add_time'] = date('Y-m-d H:i:s');

		//判断用户是否已经存在了
		$info = Db::name('auth_user')
				->field('id')
				->where('user_name', $user_name)
				->whereOr('phone', $phone)
				->find();
		if($info){
			return 115;
		}
		// print_r($data);exit;
		$rs = Db::name('auth_user')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function getMyInfo($token=''){
		$info = Db::name('auth_user')
					->field('id,user_name,auth_group_id,phone,status')
					->where('token',$token)
					->find();
		return $info;
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		if(in_array(1, $ids)){
			return 12;
		}
		$rs = Db::name('auth_user')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function getMenu($token=''){
		$info = Db::name('auth_user')
					->alias('au')
					->field('au.id as user_id, ag.id as auth_group_id, ag.group_menu_ids,ag.status')
					->join('auth_group ag','ag.id=au.auth_group_id','left')
					->where('au.token',$token)
					->find();
		if(!$info){
			return [];
		}elseif(!$info['user_id']){
			return [];
		}elseif(!$info['group_menu_ids']){
			return [];
		}

		$menuIds = json_decode($info['group_menu_ids'], true);
		$menus = Db::name('auth_menu')
					->where('id','in', $menuIds)
					->where('status',1)
					->order('sort asc, id asc')
					->select();
		$lists = getMenuTree($menus,'parent_id',0,0,1,0,'children');
		return $lists;

	}
}